package org.jb.coinche.client.business.handler.impl;

import org.jb.coinche.client.business.message.AuctionMessage;
import org.jb.coinche.client.business.message.CoincheMessage;
import org.jb.coinche.client.business.model.Auction;
import org.jb.coinche.client.business.model.AuctionTurn;
import org.jb.coinche.client.business.model.Player;
import org.jb.coinche.client.business.model.Card.Color;
import org.jb.coinche.client.gin.Old;
import org.jb.coinche.client.gui.card.AuctionPanel;
import org.jb.coinche.client.gui.card.CardList;
import org.jb.coinche.client.gui.card.CardPanel;
import org.jb.coinche.client.gui.card.SetPanel;
import org.jb.coinche.client.gui.card.StatusPanel;
import org.jb.coinche.client.gui.card.AuctionPanel.AuctionListener;
import org.jb.coinche.client.guistatemachine.GUIStateHandler;
import org.jb.coinche.client.guistatemachine.MessageSender;
import org.jb.coinche.client.user.UserLogin;

import com.google.inject.Inject;

/**
 * 
 * IHM pour les encheres
 * 
 * @author jeremie
 *
 */
public class GUIAuctionTurnHandler implements GUIStateHandler<AuctionTurn>, AuctionListener {

	/**
	 * 
	 */
	@Override
	public void disable() {
		auctionPanel.setAuctionListener(null);
		auctionPanel.setEditable(false);
		auctionPanel.setCoincheEnabled(false);
		auctionPanel.refresh();
	}

	/**
	 * 
	 */
	@Override
	public void enable() 
	{
		auctionPanel.setAuctionListener( this );
	}

	/**
	 * 
	 */
	@Override
	public void update(AuctionTurn state) {
				
		Player p = state.getGame().getPlayer( user );
		
		/*
		 * Carte
		 */
		Player self = state.getGame().getPlayer( user );
		if (self != null) {
			cardList.setCardList( self.getCards() );
		}
		cardList.setClickable(false);
		
		/*
		 * Renit des cartes jouées
		 */
		for( CardPanel cp : oldSetPanel.getCardPanels() ) {
			cp.setParameters("", "");
		}
		
		/*
		 * Enchere
		 */		
		p = state.getGame().getPlayer( user );
		if (p == null)
			p = state.getGame().getNextPlayer(null);
		
		for( CardPanel cp : setPanel.getCardPanels() ) {
			
			if (state.getWaitingPlayer().equals(p)) {
				cp.setWaiting( p.getName() );
			} else {
				Auction a = state.getPlayerAuction(p);
				
				if (a == null) {
					cp.setParameters(p.getName(), "");
				} else {
					if (a.getValue() == 0) {
						cp.setParameters( p.getName(), "Passe");
					} else {
						cp.setParameters( p.getName(), a.getValue() + " " + a.getTrump().toText() );
					}
				}
			}
			
			p = state.getGame().getNextPlayer(p);
		}
			
		/*
		 * 
		 */
		auctionPanel.setCurrentAuction( state.getCurrentAuction() );
		if (user.equals( state.getWaitingPlayer().getName() )) {
			auctionPanel.setEditable( user.equals( state.getWaitingPlayer().getName() ) );
			statusPanel.setStatus( "<b>C'est à vous de parler</b>" );

		} else {
			auctionPanel.setEditable( false );
			statusPanel.setStatus( state.getWaitingPlayer().getName() + " doit parler" );
		}
		
		/*
		 * On peut coincher si  on n'est pas l'equipe qui possede la dernière enchere
		 */
		auctionPanel.setCoincheEnabled( self != null && state.getCurrentAuction()!= null && ! self.getTeam().equals( state.getCurrentAuction().getTeam() ));
		
		auctionPanel.refresh();
		
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel.AuctionListener#onAuction(org.jb.coinche.client.business.model.Card.Color, int)
	 */
	@Override
	public void onAuction(Color color, int value) {
		AuctionMessage msg = new AuctionMessage();
		
		Auction auction = new Auction();
		auction.setTrump(color);
		auction.setValue(value);
		msg.setAuction(auction);
		
		sender.send(msg);	
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel.AuctionListener#onCoinche()
	 */
	@Override
	public void onCoinche() {
		sender.send( new CoincheMessage() );
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel.AuctionListener#onPass()
	 */
	@Override
	public void onPass() {
		AuctionMessage msg = new AuctionMessage();
		msg.setAuction( null );
		sender.send(msg);	
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#getStateClass()
	 */
	@Override
	public Class<AuctionTurn> getStateClass() {
		return AuctionTurn.class;
	}
	
	@Inject
	private MessageSender sender;
	
	@Inject
	private CardList cardList;
	
	@Inject
	private SetPanel setPanel;
	
	@Inject @Old
	private SetPanel oldSetPanel;
	
	@Inject
	private AuctionPanel auctionPanel;

	@Inject
	private StatusPanel statusPanel;
	
	@Inject @UserLogin
	private String user;
	

}
